<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>loginDemo</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="Application" style="text-align: center;">
        <!-- 标题 -->
        <h1>{{ title }}</h1>
        <!-- 创建账号密码输入组件 -->
        <div v-if="noLogin">账号：<input v-model="userName" type="text"/></div>
        <div v-if="noLogin">密码：<input v-model="password" type="password"/></div>
        <!-- 登录按钮 -->
        <div v-on:click="click" style="border-radius: 30px; width: 100px; margin: 20px auto; color: white; background-color: blue;">{{buttonTitle}}</div>
    </div>

    <script>
        const App = {
            data() {
                return {
                    title: "欢迎您，未登录",
                    noLogin: true,
                    userName: "",
                    password: "",
                    buttonTitle: "登录"
                }
            },
            methods: {
                click() {// 单击登录按钮执行的方法
                    if (this.noLogin) {
                        this.login()
                    } else {
                        this.logout()
                    }
                },
                // 定义登录操作所执行的函数
                login() {
                    // 判断账号、密码是否为空
                    if (this.userName.length > 0 && this.password.length > 0) {
                        // 模拟登录操作，进行弹窗提示
                        alert(`userName:${this.userName} password:${this.password}`)
                        // 登录后刷新页面
                        this.noLogin = false
                        this.title = `欢迎您：${this.userName}`
                        this.buttonTitle = "注销"
                    } else {
                        alert("请输入账号密码")
                    }
                },
                // 定义登出操作所执行的函数
                logout() {
                    // 清空数据
                    this.noLogin = true
                    this.title = "欢迎您，未登录"
                    this.buttonTitle = "登录"
                }
            }
        }
        Vue.createApp(App).mount("#Application")
    </script>
</body>
</html>
